const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const http = require("http");
const controller = require("./controller/controller")
const config = require("./config/config")
const mysqldb = require("./util/db")
const path = require("path")

mysqldb.createPool();
app.all('*',function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

  if (req.method == 'OPTIONS') {
    res.send(200); //让options请求快速返回//
  }
  else {
    next();
  }
});

app.use(bodyParser.json({ type: 'application/*+json' }))
app.use('/ad_api', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }))

app.get('/api/:cmd', controller.controller);
app.post('/api/:cmd', controller.controller);

app.get('/cmd/:operation', controller.operation);
app.post('/cmd/:operation', controller.operation);


var server = http.createServer(app)
server.listen(config.serverport, () => {
    console.info('[API][start][ports:' + config.serverport + ']');
});